import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/helper/http.dart';

class ChangePasswordView extends StatelessWidget {
  final TextEditingController oldPwdCtrl = new TextEditingController();
  final TextEditingController newPwdCtrl = new TextEditingController();
  final TextEditingController confirmPwdCtrl = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('修改密码'),
      ),
      backgroundColor: Colors.white,
      body: ListView(
        padding: EdgeInsets.all(20),
        children: [
          oldPwdField(),
          newPwdField(),
          confirmPwdField(),
          Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text('密码长度最少为6位'),
          ),
          Padding(
            padding: EdgeInsets.only(top: 20),
            child: btnSave(context),
          ),
        ],
      ),
    );
  }

  Widget oldPwdField() {
    return TextField(
      controller: oldPwdCtrl,
      decoration: InputDecoration(
        hintText: '请输入旧密码',
      ),
    );
  }

  Widget newPwdField() {
    return TextField(
      controller: newPwdCtrl,
      decoration: InputDecoration(
        hintText: '请输入新密码',
      ),
    );
  }

  Widget confirmPwdField() {
    return TextField(
      controller: confirmPwdCtrl,
      decoration: InputDecoration(
        hintText: '请输入新密码',
      ),
    );
  }

  Widget btnSave(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(50),
      child: Container(
        height: 40,
        child: FlatButton(
          color: Theme.of(context).primaryColor,
          textColor: Colors.white,
          child: Text('确认修改'),
          onPressed: () => save(context),
        ),
      ),
    );
  }

  Future save(BuildContext context) async {
    var message = '';
    var oldPwd = oldPwdCtrl.value.text;
    var newPwd = newPwdCtrl.value.text;
    var confirmPwd = confirmPwdCtrl.value.text;
    if (newPwd.length < 6) {
      message = '密码长度最少为6位';
    }

    if (newPwd != confirmPwd) {
      message = '两次密码输入不一致';
    }

    if (message.isNotEmpty) {
      showAlert(context, message);
      return;
    }

    var request = new Map<String, dynamic>();
    request['userId'] = Auth.instance.userId;
    request['password'] = oldPwd;
    request['newPassword'] = newPwd;
    request['againPassword'] = confirmPwd;
    var res = await Http.instance.post(
      '/usercenter-app/user/modifypassword',
      params: request,
    );
    if (res.code == 200) {
      showAlert(context, '修改密码成功', callback: () {
        Navigator.of(context).pop();
      });
    } else {
      showAlert(context, res.msg);
    }
  }

  void showAlert(BuildContext context, String text, {Function callback}) {
    showDialog(
        context: context,
        builder: (cxt) {
          return AlertDialog(
            title: Text('提示'),
            content: Text(text),
            actions: [
              FlatButton(
                textColor: Theme.of(context).primaryColor,
                child: Text('确定'),
                onPressed: () {
                  Navigator.of(context).pop();
                  if (callback != null) {
                    callback();
                  }
                },
              ),
            ],
          );
        });
  }
}
